import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# List of N values
N_values = [6, 11, 21, 41, 81]

# Create a new figure for the combined plot
plt.figure(figsize=(10, 8))

# Plot the exact function f(x) = 1 / (1 + 25 * x^2) once
x_exact = np.linspace(-1, 1, 400)
y_exact = 1 / (1 + 25 * x_exact**2)
plt.plot(x_exact, y_exact, 'k--', label='Exact function')

# Loop over the N values and generate plots for each
for N in N_values:
    # Read the data for the current N value
    data = pd.read_csv(f'spline_A_{N}.csv', header=None)  # Assuming the files are named spline_A_6.csv, spline_A_11.csv, etc.

    # Plot the spline interpolation data
    plt.plot(data[0], data[1], label=f'N={N}')

# Adding the title, labels, and grid
plt.title('Problem A: Spline Interpolation Comparison')
plt.xlabel('X')
plt.ylabel('f(X)')
plt.grid(True)

# Add legend to the plot
plt.legend()

# Save the combined plot as a PNG file
plt.savefig('../figure/A_combined.png')

# Show the combined plot
plt.show()